<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PibsController
 *
 * @author Regan
 */
App::import('Vendor', 'importer', array('file'=> 'MDBImport' . DS . 'Exporter.php'));

class PibsController extends AppController {
    public function index()
    {
        $this->loadModel('Header');
        $allHeader = $this->Header->find('all');
        
        $this->set('allHeader', $allHeader);
    }
    
    public function view()
    {
        
    }
    
    public function cusresexportconfirm($CAR)
    {
        if(isset($this->request->data['generate']))
        {
            $this->redirect(array('action' => 'cusresexport', $this->request->data['Header']['CAR']));
        }
        else if(isset($this->request->data['cancel']))
        {
            $this->redirect(array('action' => 'index'));
        }
        else
        {
            if($CAR == null)
            {
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash('No AJU Tidak di supply');
            }

            $this->loadModel('Header');
            $objHeader = $this->Header->find('first', array(
                'conditions' => array(
                    'Header.CAR' => $CAR
                )
            ));

            $this->request->data = $objHeader;
        }
    }
    
    public function cusresexport($CAR)
    {
        //$this->view('cusresexportconfirm');
        
        $objExporter = new Exporter();
        $result = $objExporter->CUSRESExport($CAR);
        
        if(file_exists($result['fileResult'])) 
        { 
            $this->response->file($result['fileResult']); 
            $this->response->download($result['fileResult']);
        }
        
        return $this->response;
        //$this->redirect(array('action' => 'index'));
    }
}
